package com.iot08.api.service.active;

import com.alibaba.fastjson.JSONObject;
import com.iot08.common.entity.app.active.DailyActive;
import com.iot08.common.service.base.BaseService;
import com.iot08.common.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * @author weichao 1299101858@qq.com
 * @version 1.0
 * @date 4/29/22 2:40 PM
 */
@Slf4j
@Service
public class DailyActiveService extends BaseService<DailyActive> {
    public void add(DailyActive baseActive) {
        String td = DateUtil.dateToStr(new Date(), DateUtil.FORMAT_4);
        baseActive.setDate(td);
        try{
            //获取当天零时的时间
            SimpleDateFormat sdf = new SimpleDateFormat( DateUtil.FORMAT_4);
            Date d = sdf.parse(td);
            TimeZone curTimeZone = TimeZone.getTimeZone("GMT+8");
            Calendar c = Calendar.getInstance(curTimeZone);
            c.setTime(d);
            c.set(Calendar.HOUR_OF_DAY, 0);
            c.set(Calendar.MINUTE, 0);
            c.set(Calendar.SECOND, 0);
            Date z = c.getTime();
//        System.out.println(sdf.format(z));
            baseActive.setExpiryAt(z);
        }catch (Exception error){
            log.error("获取当天零时的时间失败:"+error.getMessage(), error);
        }

        this.save(baseActive);
    }
}
